

export default class OSMBuildingUtil{
    constructor(viewer){
        this.viewer =viewer;
        this.osmBuildingsTileset =undefined;
        this.handler =undefined;
    }
    loadOSMBuildings(){
        this.osmBuildingsTileset =Cesium.createOsmBuildings();
        this.viewer.scene.primitives.add(this.osmBuildingsTileset);
        this.viewer.scene.camera.setView({
            destination: Cesium.Cartesian3.fromDegrees(-122.3472, 47.598, 370),
            orientation: {
              heading: Cesium.Math.toRadians(10),
              pitch: Cesium.Math.toRadians(-10),
            },
          });


    }
    colorByMaterial() {
        if(this.handler){        this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);}

        if(this.osmBuildingsTileset){
            this.osmBuildingsTileset.style = new Cesium.Cesium3DTileStyle({
                defines: {
                  material: "${feature['building:material']}",
                },
                color: {
                  conditions: [
                    ["${material} === null", "color('white')"],
                    ["${material} === 'glass'", "color('skyblue', 0.5)"],
                    ["${material} === 'concrete'", "color('grey')"],
                    ["${material} === 'brick'", "color('indianred')"],
                    ["${material} === 'stone'", "color('lightslategrey')"],
                    ["${material} === 'metal'", "color('lightgrey')"],
                    ["${material} === 'steel'", "color('lightsteelblue')"],
                    ["true", "color('white')"], // This is the else case
                  ],
                },
              });
        }
      }
      colorByDistanceToCoordinate() {
          if(!this.handler){
            this.handler =new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
          }

        this.handler.setInputAction((movement)=>{
            this.viewer.selectedEntity =undefined;
            let pickedBuildings =this.viewer.scene.pick(movement.position);
            if(pickedBuildings){
                let pickedLatitude =pickedBuildings.getProperty("cesium#latitude");
                let pickedLongitude =pickedBuildings.getProperty("cesium#longitude");
                colorByDistance(pickedLatitude,pickedLongitude);
            }
        },Cesium.ScreenSpaceEventType.LEFT_CLICK);
        let that =this;
        function colorByDistance(pickedLatitude,pickedLongitude){
            that.osmBuildingsTileset.style = new Cesium.Cesium3DTileStyle({
                defines: {
                distance:
                "distance(vec2(${feature['cesium#longitude']}, ${feature['cesium#latitude']}), vec2(" +
                pickedLongitude +
                "," +
                pickedLatitude +
                "))",
            },
            color: {
                conditions: [
                ["${distance} > 0.014", "color('blue')"],
                ["${distance} > 0.010", "color('green')"],
                ["${distance} > 0.006", "color('yellow')"],
                ["${distance} > 0.0001", "color('red')"],
                ["true", "color('white')"],
                ],
                },
            });

        }
      }
}